<template>
	<view class="search-wrapper">
		<view class="search-bar">
			<uni-search-bar placeholder="搜索" focus v-model="params.keyword" 
				@confirm="onSearch" @cancel="clear" @clear="clear">
			</uni-search-bar>
		</view>
		<view class="content-wrapper" v-if="!classList.length && noData == false">
			<!-- 最近搜索 -->
			<view class="history-wrapper" v-if="history.length">
				<view class="title-wrapper">
					<view>最近搜索</view>
					<uni-icons @click="clearChange" type="trash-filled" size="24"></uni-icons>
				</view>
				<view class="tag-wrapper">
					<view @click="tagChange(item)" class="tag" v-for="(item,index) in history" :key="index">
						{{item}}
					</view>
				</view>
			</view>
			<!-- 热门搜索 -->
			<view class="hot-wrapper history-wrapper">
				<view class="hot-wrapper title-wrapper">
					热门搜索
				</view>
				<view class="tag-wrapper">
					<view @click="tagChange(item)" class="tag" v-for="(item,index) in hotList" :key="index">
						{{item}}
					</view>
				</view>
			</view>
			
			<view class="empty-wrapper">
				暂无搜索内容~
			</view>
		</view>
		<view class="data-wrapper" v-else>
			<view class="list-wrapper" >
				<view class="pic-wrapper" v-for="item in classList" @click="gopreview(item._id)">
					<image class="pic" :src="item.smallPicurl" mode="aspectFill"></image>
				</view>
				
			</view>
			<view class="loadingLayout">
				<uni-load-more :status="status"></uni-load-more>
			</view>
		</view>

	</view>
</template>

<script setup>
import { ref } from 'vue';
import {onReachBottom} from '@dcloudio/uni-app'
import {apiSearchWall} from '@/api/index.js'

const history = ref(uni.getStorageSync('historySearch') || [])
const hotList = ref(['美女','帅哥','宠物','卡通'])

const params = ref({
	pageNum:1,
	pageSize:15,
	keyword:'',
})
//数据是否加载完毕  more/loading/noMore
const status = ref('loading')
const total = ref(0)
const classList = ref([])
const noData = ref(false)
//搜索
const onSearch = ()=>{
	history.value = [...new Set([params.value.keyword,...history.value])].slice(0,10)
	uni.setStorageSync('historySearch',history.value)
	reset()
	
	getData()
}
//重置
const reset = ()=>{
	noData.value = false
	classList.value = []
	total.value = 0
	params.value.pageNum = 1
}
const tagChange = (item)=>{
	params.value.keyword = item
	onSearch()
}
//清除
const clear = ()=>{
	params.value.keyword = ''
	reset()
}

//清除搜索历史
const clearChange = ()=>{
	uni.removeStorageSync('historySearch')
	history.value = []
}

//请求数据
const getData = async()=>{
	uni.showLoading({})
	const res = await apiSearchWall(params.value)
	uni.hideLoading()
	classList.value = [...classList.value,...res.data]
	total.value = res.total
	if(classList.value.length < params.value.pageSize){
		status.value = 'noMore'
	}
	if(res.data.length == 0){
		noData.value = false
	}else{
		noData.value = true
	}

}

//触底加载
onReachBottom(()=>{
	if(total.value == classList.value.length){
		status.value = 'noMore'
		return
	}
	params.value.pageNum++
	getData()
})
</script>

<style lang="scss" scoped>
.search-wrapper{
	width: 100%;
	padding: 0 30rpx;
	box-sizing: border-box;
	.search-bar{
		width: 100%;
	}
	
	.content-wrapper{
		width: 100%;
		.history-wrapper{
			
			margin-top: 20rpx;
			.title-wrapper{
				display: flex;
				justify-content: space-between;
				font-size: 30rpx;
				color: #929292;
				margin-bottom: 30rpx;
			}
			.tag-wrapper{
				display: flex;
				flex-wrap: wrap;
				gap: 20rpx; /* 设置 item 之间的间隔 */
				.tag{
					background: #f4f4f4;
					padding: 10rpx 25rpx;
					border-radius: 30rpx;
					color: #2b2b2b;
					margin-bottom: 20rpx;
				}
			}
		}
		.hot-wrapper{
			margin-top: 30rpx;
		}
		.empty-wrapper{
			width: 100%;
			height: 40vh;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #929292;
		}
	}
	.list-wrapper{
		width: 100%;
		margin: 0 auto;
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 5rpx;
		.pic-wrapper {
			width: 100%;
			height: 340rpx;
		
			.pic {
				width: 100%;
				height: 100%;
			}
		}
	}
}
</style>